﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using VipCo.Data;
using VipCo.Entities;
using System.Data;


public partial class Admin_SlideShow_QLSlideShow : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        imgViewer.ImageUrl = H_AnhDaiDien.Value.Length > 0 ? Common.sPathUpSlideShow + H_AnhDaiDien.Value : "~/Images/no_image.jpg";
        if (!Page.IsPostBack)
        {
            Pager1.CurrentIndex = 1;
            Bind_Data(Pager1.CurrentIndex);
        }
    }
    protected void Bind_Data(int PageNo)
    {
        int TotalRows = 0;
        string WhereClause = "1=1";
        DataSet dsBookCategories = DataRepository.Provider.ExecuteDataSet("SlideShow_GetPaged", WhereClause, "Id DESC", PageNo - 1, Pager1.PageSize);
        gridCatalog.DataSource = dsBookCategories.Tables[0];
        gridCatalog.DataBind();
        try
        {
            TotalRows = Convert.ToInt32(dsBookCategories.Tables[1].Rows[0][0]);
        }
        catch (Exception)
        {
        }
        Pager1.ItemCount = TotalRows;
        if (TotalRows <= Pager1.PageSize)
            Pager1.Visible = false;
        else
            Pager1.Visible = true;

    }
    protected void Pager1_Command(object sender, CommandEventArgs e)
    {
        try
        {
            int currnetPageIndex = Convert.ToInt32(e.CommandArgument);
            Pager1.CurrentIndex = currnetPageIndex;
            Bind_Data(currnetPageIndex);
        }
        catch (Exception ex)
        {
            Common.WriteLog("QLSlideShow.aspx.cs", "Pager1_Command()", ex.ToString());
        }
    }

    protected void cmdSave_Click(object sender, ImageClickEventArgs e)
    {
        bool updateStatus = false;
        try
        {
            SlideShow ent_SlideShow = new SlideShow();
            ent_SlideShow.Id = Convert.ToInt32(H_CatalogId.Value);
            ent_SlideShow.TieuDe = Common.StandardString(txtTieuDe.Text);
            ent_SlideShow.LienKet = Common.StandardString(txtLienKet.Text);
            ent_SlideShow.TomTat = Common.StandardString(txtTomTat.Text);
            ent_SlideShow.PhanTram = Convert.ToInt32(txtPhanTram.Text);
            ent_SlideShow.UuTien = Convert.ToInt32(txtUuTien.Text);
            ent_SlideShow.TrangThai = chkTrangThai.Checked;
            if (H_AnhDaiDien.Value.Length == 0)
            {
                ent_SlideShow.AnhDaiDien = "no_image.jpg";
            }
            else
            {
                ent_SlideShow.AnhDaiDien = Common.StandardString(H_AnhDaiDien.Value);
            }
            ent_SlideShow.NgayCapNhat = Common.GetServerDateTime();
            if (ent_SlideShow.Id == 0)
            {
                ent_SlideShow.Id = 0;
                updateStatus = DataRepository.SlideShowProvider.Insert(ent_SlideShow);
            }
            else
            {
                updateStatus = DataRepository.SlideShowProvider.Update(ent_SlideShow);
            }
            if (updateStatus)
            {
                ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), Constants.THANHCONG, Constants.SLIDESHOW_CAPNHAT_THANHCONG, true);
                Pager1.CurrentIndex = 1;
                Bind_Data(Pager1.CurrentIndex);
                cmdReset_Click(sender, e);
            }
            else
                ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), Constants.THATBAI, Constants.SLIDESHOW_CAPNHAT_THATBAI, true);
        }
        catch (Exception ex)
        {
            Common.WriteLog("QLSlideShow.aspx.cs", "cmdSave_Click", ex.ToString());
            ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), Constants.THATBAI, Constants.SLIDESHOW_CAPNHAT_THATBAI, true);
        }
    }

    protected void cmdReset_Click(object sender, ImageClickEventArgs e)
    {
        txtTieuDe.Text = "";
        txtTomTat.Text = "";
        txtLienKet.Text = "";
        txtPhanTram.Text = "";
        txtUuTien.Text = "";
        H_AnhDaiDien.Value = "no_image.jpg";
        H_CatalogId.Value = "0";
    }

    protected void gridCatalog_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            ImageButton l = (ImageButton)e.Row.FindControl("cmdDelete");
            l.Attributes.Add("onclick", "javascript:return " + "confirm('Bạn chắc chắn muốn xóa thông tin này?');");

            e.Row.Attributes["onmouseover"] = "this.style.cursor='hand';this.style.backgroundColor='WhiteSmoke';";
            e.Row.Attributes["onmouseout"] = "this.style.backgroundColor='White';";
        }
    }

    protected void gridCatalog_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Select")
        {
            try
            {
                H_CatalogId.Value = e.CommandArgument.ToString();
                SlideShow ent_SlideShow = DataRepository.SlideShowProvider.GetById(Convert.ToInt32(H_CatalogId.Value));
                if (ent_SlideShow != null)
                {
                    txtTieuDe.Text = ent_SlideShow.TieuDe;
                    txtTomTat.Text = ent_SlideShow.TomTat;
                    txtLienKet.Text = ent_SlideShow.LienKet;
                    txtPhanTram.Text = ent_SlideShow.PhanTram.ToString();
                    imgViewer.ImageUrl = "~/Upload/SlideShow/" + ent_SlideShow.AnhDaiDien;
                    txtUuTien.Text = ent_SlideShow.UuTien.ToString();
                }
            }
            catch (Exception ex)
            {
                Common.WriteLog("QLSlideShow.aspx.cs", "gridCatalog_RowCommand - Select", ex.ToString());
            }
        }
        if (e.CommandName == "Delete")
        {
            try
            {
                int IdDelete = Convert.ToInt32(e.CommandArgument.ToString());
                bool deleteStatus = DataRepository.SlideShowProvider.Delete(IdDelete);
                if (!deleteStatus)
                    ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), Constants.THATBAI, Constants.SLIDESHOW_XOA_THATBAI, true);
                else
                    ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), Constants.THATBAI, Constants.SLIDESHOW_XOA_THANHCONG, true);
            }
            catch (Exception ex)
            {
                Common.WriteLog("QLSlideShow.aspx.cs", "gridCatalog_RowCommand - Delete", ex.ToString());
                ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), Constants.THATBAI, Constants.SLIDESHOW_XOA_THATBAI, true);
            }
        }
    }

    protected void gridCatalog_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        Pager1.CurrentIndex = 1;
        Bind_Data(Pager1.CurrentIndex);
        txtTieuDe.Text = "";
        txtTomTat.Text = "";
        txtLienKet.Text = "";
        txtPhanTram.Text = "";
        txtUuTien.Text = "";
        H_AnhDaiDien.Value = "no_image.jpg";
        H_CatalogId.Value = "0";
    }

}